import React from 'react';
import { IndexRoute, Route } from 'react-router';
import App from './containers/App';
import Welcome from './pages/welcome';

const Login = (location, cb) => {
  require.ensure([], require => {
    cb(null, require('./containers/App/login').default)
  }, 'login');
};

const all = (location, cb) => {
  require.ensure([], require => {
    cb(null, require('./pages/admin/all').default)
  }, 'all');
};

const add = (location, cb) => {
  require.ensure([], require => {
    cb(null, require('./pages/admin/add').default)
  }, 'add');
};

const edit = (location, cb) => {
  require.ensure([], require => {
    cb(null, require('./pages/admin/edit').default)
  }, 'edit');
};

const lesson = (location, cb) => {
  require.ensure([], require => {
    cb(null, require('./pages/open/lesson/index').default)
  }, 'lesson');
};

const audio = (location, cb) => {
  require.ensure([], require => {
    cb(null, require('./pages/open/audio').default)
  }, 'audio');
};

const audioDetail = (location, cb) => {
  require.ensure([], require => {
    cb(null, require('./pages/open/audio/detail').default)
  }, 'audioDetail');
};

const video = (location, cb) => {
  require.ensure([], require => {
    cb(null, require('./pages/open/video').default)
  }, 'video');
};

const videoDetail = (location, cb) => {
  require.ensure([], require => {
    cb(null, require('./pages/open/video/detail').default)
  }, 'videoDetail');
};

/* 进入路由的判断*/
function isLogin(nextState, replaceState) {
  const token = sessionStorage.getItem('token');
  if (!token || token && Date.now() - token > 7200000) {
    sessionStorage.clear();
    replaceState('/login')
    // hashHistory.push('/login')
  }
}

const routes = (
  <Route>
    <Route path="/" component={App} onEnter={isLogin}>
      <IndexRoute component={Welcome} />
      <Route path="/all" getComponent={all} />
      <Route path="/add" getComponent={add} />
      <Route path="/edit" getComponent={edit} />
    </Route>
    <Route path="/login" getComponent={Login}></Route>
    <Route path="/open/lesson/:openId" getComponent={lesson}></Route>
    <Route path="/open/audio" getComponent={audio}></Route>
    <Route path="/open/audio/detail" getComponent={audioDetail}></Route>
    <Route path="/open/video" getComponent={video}></Route>
    <Route path="/open/video/detail" getComponent={videoDetail}></Route>
  </Route>
);

export default routes;
